package com.lashou.chunshen.lashoutuangou.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.lashou.chunshen.lashoutuangou.R;
import com.lashou.chunshen.lashoutuangou.bean.HeaderViewBean;
import com.lashou.chunshen.lashoutuangou.bean.HomeCategory;
import com.lashou.chunshen.lashoutuangou.tools.PicassoUtils;
import com.lashou.chunshen.lashoutuangou.tools.ToastUtil;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 首页分类gridView
 * Created by momo on 2016/9/6.
 */
public class GridViewAdapter extends BaseAdapter {
    List<HomeCategory.ResultsBean> mDatas;
    private LayoutInflater mLayoutInflater;
    Context context;
    /**
     * 页数下标,从0开始
     */
    private int mIndex;
    /**
     * 每页显示最大条目个数 ,默认是dimes.xml里 HomePageHeaderColumn 属性值的两倍
     */
    private int mPageSize;


    public GridViewAdapter(Context context, List<HomeCategory.ResultsBean> mDatas, int mIndex) {
        this.mDatas = mDatas;
        this.context = context;
        mLayoutInflater = LayoutInflater.from(context);
        this.mIndex = mIndex;
        mPageSize = 8;
    }

    /**
     * 先判断数据集的大小是否足够显示满本页？mDatas.size() > (mIndex+1)*mPageSize,
     * 如果够，则直接返回每一页显示的最大条目个数mPageSize,
     * 如果不够，则有几项返回几,(mDatas.size() - mIndex * mPageSize);
     */
    @Override
    public int getCount() {
        int count = mDatas.size() > (mIndex + 1) * mPageSize ? mPageSize : (mDatas.size() - mIndex * mPageSize);
        return count;

    }

    @Override
    public Object getItem(int position) {
        int itemId = position + mIndex * mPageSize;
        return mDatas.get(itemId);
    }

    @Override
    public long getItemId(int position) {
        return position ;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.i("TAG", "position:" + position);
        ViewHolderClass vh = null;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.home_category, parent, false);
            vh = new ViewHolderClass(convertView);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolderClass) convertView.getTag();
        }
        /**
         * 在给View绑定显示的数据时，计算正确的position = position + mIndex * mPageSize，
         */
        int pos = position + mIndex * mPageSize;
        final HomeCategory.ResultsBean bean = mDatas.get(pos);
        if (null != bean) {
            PicassoUtils.loadImageViewHolder(context, bean.getImg_url(), R.drawable.home_class_default_icon, vh.imageView);
            vh.textView.setText(bean.getCate_name());
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.show(context,bean.getCate_name());
            }
        });
        return convertView;
    }


    public class ViewHolderClass {
        @InjectView(R.id.iv_home_category)
        ImageView imageView;
        @InjectView(R.id.tv_home_category)
        TextView textView;

        public ViewHolderClass(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
